package site.lanmushan.framework.util.bean;



import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * @author Administrator
 */
public class SlashBeanUtil {
    public static <T> T copyProperties(Object obj, Class<T> aClass) {
        if (obj == null) {
            return null;
        }
        try {
            T t = null;
            t = aClass.newInstance();
            BeanUtils.copyProperties(obj, t);
            return t;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> List<T> copyListProperties(Collection collection, Class<T> aClass) {
        if (collection == null) {
            return null;
        }
        Iterator<T> it = collection.iterator();
        List<T> resultList = new ArrayList<>(collection.size());
        while (it.hasNext()) {
            T next = it.next();
            resultList.add(SlashBeanUtil.copyProperties(next, aClass));
        }
        return resultList;
    }

}
